﻿using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace OneWorks.Web.Common.BaiDu.Geoconv
{
    public enum FromType
    {
        /// <summary>
        /// GPS设备获取的角度坐标;
        /// </summary>
        GPSPoi = 1,
        /// <summary>
        ///   GPS获取的米制坐标、sogou地图所用坐标;
        /// </summary>
        GPSPoiByM = 2,
        /// <summary>
        /// google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标
        /// </summary>
        GoogleOrSosoOrAliyunOrAmap = 3,
        /// <summary>
        /// google地图、soso地图、aliyun地图、mapabc地图和amap地图所用米制坐标
        /// </summary>
        GoogleOrSosoOrAliyunOrAmapByM = 4,

        /// <summary>
        ///  百度地图采用的经纬度坐标
        /// </summary>
        BaiDuPoi = 5,
        /// <summary>
        /// 百度地图采用的米制坐标
        /// </summary>
        BaiDuPoiByM = 6,
        /// <summary>
        /// mapbar地图坐标
        /// </summary>
        Mapbar = 7,
        /// <summary>
        ///   51地图坐标
        /// </summary>
        Poi51 = 8
    }

    public enum ToType
    {
        /// <summary>
        ///  5：bd09ll(百度经纬度坐标)
        /// </summary>
        BD09ll = 5,
        /// <summary>
        /// bd09mc(百度米制经纬度坐标)
        /// </summary>
        BD09mc = 6
    }

    public class GeoconvParams
    {
        /// <summary>
        /// 用户的访问密钥，必填项。v2之前该属性为key。
        /// </summary>
        public string Ak;

        public FromType From;

        public ToType To;

        /// <summary>
        /// json或者xml
        /// </summary>
        public string Output;


        public string ToString(ref List<GeoconvPOI> geoconvPois)
        {
            StringBuilder url = new StringBuilder("coords=");
            int i = 0;
            foreach (GeoconvPOI poi in geoconvPois)
            {
                poi.Index = i;
                url.AppendFormat("{0},{1}{2}", poi.Lng, poi.Lat, i == geoconvPois.Count - 1 ? "" : ";");
                i++;
            }
            url.AppendFormat("&ak={0}", Ak);
            url.AppendFormat("&from={0}", System.Convert.ToInt32(From));
            url.AppendFormat("&to={0}", System.Convert.ToInt32(To));
            url.AppendFormat("&output={0}", Output);
            return url.ToString();
        }
    }


    public class GeoconvResults
    {
        /// <summary>
        /// 返回结果状态值， 成功返回0，其他值请查看附录。
        /// </summary>
        [JsonProperty(PropertyName = "status")]
        public Status Status;

        /// <summary>
        /// 返回结果状态值， 成功返回0，其他值请查看附录。
        /// </summary>
        [JsonProperty(PropertyName = "result")]
        public List<GeoconvPOI> Result;


        [JsonProperty(PropertyName = "message")]
        public string ErrorMsg;
    }

    public class GeoconvPOI
    {
        [JsonIgnore]
        public int Index;

        [JsonIgnore]
        public int Key;

        /// <summary>
        /// 纬度值
        /// </summary>
        [JsonProperty(PropertyName = "y")]
        public string Lat;
        /// <summary>
        /// 经度值
        /// </summary>
        [JsonProperty(PropertyName = "x")]
        public string Lng;
    }
}
